package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.utils.GetJsonDataUtil;

import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class FloorListSmallAdapter extends AppAdapter<FloorBean> {
    public FloorListSmallAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public FloorListSmallAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FloorListSmallAdapter.ViewHolder();
    }

    @Override
    public void setData(@Nullable List<FloorBean> data) {
        super.setData(data);
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private ImageView closeImage;
        private ImageView roomIconImage;
        private TextView roomName;
        private TextView roomInfo;
        private ViewHolder() {
            super(R.layout.item_room_list_small);
            closeImage = findViewById(R.id.btn_all_close);
            roomIconImage = findViewById(R.id.room_icon_image);
            roomName = findViewById(R.id.tv_name);
            roomInfo = findViewById(R.id.tv_device_num);
        }

        @Override
        public void onBindView(int position) {
            FloorBean floorBean = getItem(position);
            roomIconImage.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_room_floor));
            roomName.setText(floorBean.getFloorName());
            List<DeviceBean> devices;
            List<SceneBean> scenes;
            if(floorBean.getType() == 1){
                devices = LLDeviceDBManager.getInstance(getContext()).getAvailableDevices();
                scenes= LLSceneDBManager.getInstance(getContext()).getAll();
            }else{
                devices = LLDeviceDBManager.getInstance(getContext()).getHomeAvailableDeviceByFloor(floorBean.getFloorSerialNo());
                scenes = LLSceneDBManager.getInstance(getContext()).getSceneByFloor(floorBean.getFloorSerialNo());
            }

            Boolean uncloseDevice = false;
            int deviceSize = 0;
            for(DeviceBean deviceBean : devices){
                if(!deviceBean.getIsDisable()){
                    deviceSize++;
                }
                if(deviceBean.getIsOpen() && deviceBean.getType().equals("light") && deviceBean.getDeviceGroup() == 0){
                    uncloseDevice = true;
                }
            }
            closeImage.setVisibility(uncloseDevice ? View.VISIBLE : View.GONE);
            int sceneSize = scenes == null ? 0 : scenes.size();
            String text = "";
            if(deviceSize > 0){
                text = getString(R.string.device_count_holder, deviceSize);
            }
            if(sceneSize > 0){
                if(TextUtils.isEmpty(text)){
                    text = getString(R.string.scene_count_holder, sceneSize);
                }else{
                    text += "，" + getString(R.string.scene_count_holder, sceneSize);
                }
            }
            if(TextUtils.isEmpty(text)){
                text = "暂无设备和场景";
            }
            roomInfo.setText(text);
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}